﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Recaptcha;
using System.Configuration;
using System.Diagnostics.CodeAnalysis;

namespace mBlog.Helpers
{
    public class RecaptchaValidatorWrapper
    {
        private const string ChallengeFieldKey = "recaptcha_challenge_field";
        private const string ResponseFieldKey = "recaptcha_response_field";

        public static bool Validate(HttpContextBase context)
        {
            var request = context.Request;

            var validator = new RecaptchaValidator
            {
                PrivateKey = ConfigurationManager.AppSettings["ReCaptchaPrivateKey"],
                RemoteIP = request.UserHostAddress,
                Challenge = request.Form[ChallengeFieldKey],
                Response = request.Form[ResponseFieldKey]
            };
            try
            {
                RecaptchaResponse response = validator.Validate();
                return response.IsValid;
            }
            catch
            {
                return true;
            }
        }
    }
}